package com.tools.service.impl;

import cn.hutool.db.sql.Order;
import com.google.common.collect.Lists;
import com.tools.model.Request.ResultRequest;
import com.tools.model.entity.ResultEntity;
import com.tools.repository.ResultRepository;
import com.tools.service.ResultService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.xml.transform.Result;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@RequiredArgsConstructor
public class ResultServiceImpl implements ResultService {
    private final ResultRepository resultRepository;
    @Override
    public ResultRequest getResultByUserTaskId(String userTaskId) {
       List<ResultEntity> resultEntityList= resultRepository.findAllByTaskUserIdOrderByCreateTime(userTaskId);
        ResultRequest resultRequest =new ResultRequest();


        List<String> categories = Lists.newArrayList();
        List<Double> data = Lists.newArrayList();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String Date =new String();

        for(int i=0;i<resultEntityList.size();i++){
            if(i==0){
                Date = simpleDateFormat.format(resultEntityList.get(0).getCreateTime());
            }else {
                if (Date.equals(simpleDateFormat.format(resultEntityList.get(i).getCreateTime()))) {
                    continue;
                }
            }
            String SCategories=resultEntityList.get(i).getCreateTime().toString();
            Double DData=Double.parseDouble(resultEntityList.get(i).getOpenDate());
            categories.add(SCategories);
            data.add(DData);
            Date =simpleDateFormat.format(resultEntityList.get(i).getCreateTime());
        }

       resultRequest.setCategories(categories);
        resultRequest.setData(data);
        return resultRequest;
    }

    @Override
    public void save(ResultEntity resultEntity) {
        resultRepository.save(resultEntity);
    }
}
